#!/bin/sh
##
G_RZ="/tmp/log/koolproxy.log"   #日志位置
G_UP="rules.ngrok.wang"  #规则更新服务器检测
G_WZ="/bin/01kool.sh"
G_ML="/tmp/G_RAM/koolproxy/"  #koolproxy安装目录
G_JG=10          #监控间隔(秒)
G_CS=720     #多少次后重启守护脚本
G_PID="/tmp/log/01kool.pid"
sleep 1
cat "$G_PID" | xargs kill -s 9
echo $$ > "$G_PID"
echo $(date +"%Y-%m-%d %H:%M:%S")    "守护脚本PID【"$$"】" >> "$G_RZ"
G_IP=180.76.76.76
G_BL=0
G_AZ=0
sleep 150
while [ "1" ];
do
sleep $G_JG
let G_BL=$G_BL+1
 if [ "$G_BL" -ge "$G_CS" ];then
G_BLL=0
G_JGG=5
while [ "$G_BLL" -eq 0 ] ;
do
sleep $G_JGG
if ping -w 1 -c 1 "$G_IP"
then
if ping -w 1 -c 1 "$G_UP"
then
G_BLL=1
else
echo -e "\n"$(date +"%Y-%m-%d %H:%M:%S")         "【警告】无法连接KoolProxy服务器！本轮不更新KoolProxy……" >> "$G_RZ"
G_BLL=2
fi
else
if [ "$G_JGG" -eq 5 ];then
"$G_ML"init.d/koolproxy stop&
echo $(date +"%Y-%m-%d %H:%M:%S")         "【错误】无法连接互联网！已自动监控网络。已关闭KoolProxy，恢复恢复网络后更新、重启KoolProxy……" >> "$G_RZ"
fi
G_BLL=0
G_JGG=59
fi
done
if [ "$G_BLL" -ne 2 ];then
/usr/share/koolproxy/koolproxyupdate&
else
"$G_ML"init.d/koolproxy restart&
fi
echo $(date +"%Y-%m-%d %H:%M:%S")  "守护脚本运行满【"$G_BL"】次，重启KoolProxy成功。" >> "$G_RZ"
G_BL=0
sh "$G_WZ"&
echo $(date +"%Y-%m-%d %H:%M:%S")        "守护脚本【自动重启】成功！" >> "$G_RZ"
exit 0
fi
G_ZF=`ps | grep "koolproxy -d" | wc -l`
if [ "$G_ZF" -ne 3 ];then
"$G_ML"init.d/koolproxy restart&
echo $(date +"%Y-%m-%d %H:%M:%S")    "【错误】 守护脚本运行到第【"$G_BL"】次时，发现KoolProxy 崩溃！守护脚本已重新启动KoolProxy ！" >> "$G_RZ"
 fi
done